home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / kernel / sys / sysPrintf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-19  |  6.4 KB  |  313 lines

  1. /*
  2.  *  sysPrintf --
  3.  *
  4.  *      Perform all formatted printing to the console.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  *
  15.  */
  16.  
  17. #ifndef lint
  18. static char rcsid[] = "$Header: /cdrom/src/kernel/Cvsroot/kernel/sys/sysPrintf.c,v 9.14 92/08/18 11:26:40 jhh Exp $ SPRITE (Berkeley)";
  19. #endif not lint
  20.  
  21. #include <sprite.h>
  22. #include <machMon.h>
  23. #include <sync.h>
  24. #include <mach.h>
  25. #include <main.h>
  26. #include <fs.h>
  27. #include <varargs.h>
  28. #include <stdio.h>
  29. #include <sys.h>
  30. #include <sysInt.h>
  31. #include <dbg.h>
  32. #include <dev.h>
  33. #include <devVid.h>
  34.  
  35. /*
  36.  * Calls to panic and printf are protected.
  37.  */
  38. Sync_Semaphore    sysPrintMutex = Sync_SemInitStatic("sysPrintMutex");
  39.  
  40. /*
  41.  * Set during a panic to prevent recursion.
  42.  */
  43. Boolean    sysPanicing = FALSE;
  44.  
  45. /*
  46.  * To turn off all printfs during booting.
  47.  */
  48. Boolean sys_DontPrint = FALSE;
  49.  
  50. /*
  51.  * Used to keep track of bytes written.
  52.  */
  53. static int bytesWritten;
  54.  
  55. static void writeProc _ARGS_((FILE *stream, Boolean flush));
  56.  
  57. /*
  58.  * vprintf buffer.
  59.  */
  60. #define    STREAM_BUFFER_SIZE    512
  61. static unsigned char streamBuffer[STREAM_BUFFER_SIZE];
  62.  
  63.  
  64. /*
  65.  * ----------------------------------------------------------------------------
  66.  *
  67.  * writeProc --
  68.  *
  69.  *      Stream writeProc - flushes data to syslog. 
  70.  *
  71.  * Results:
  72.  *    None
  73.  *
  74.  * Side effects:
  75.  *      None.
  76.  *
  77.  * ----------------------------------------------------------------------------
  78.  */
  79. /*ARGSUSED*/
  80. static void
  81. writeProc(stream, flush)
  82.     FILE *stream;
  83.     Boolean flush;
  84. {
  85.     Fs_IOParam    io;
  86.     Fs_IOReply    reply;
  87.  
  88.     bzero((char *)&io, sizeof(io));
  89.     io.buffer = (Address) stream->buffer;
  90.     io.length = stream->lastAccess + 1 - stream->buffer;
  91.     bzero((char *)&reply, sizeof(reply));
  92.  
  93.     if (io.length > 0) { 
  94.     (void)Dev_SyslogWrite((Fs_Device *) NIL, &io, &reply);
  95.     stream->lastAccess = stream->buffer - 1;
  96.     stream->writeCount = stream->bufSize;
  97.     bytesWritten += reply.length;
  98.     }
  99. }
  100.  
  101.  
  102. /*
  103.  * ----------------------------------------------------------------------------
  104.  *
  105.  * vprintf --
  106.  *
  107.  *    Printing routine that is called from varargs procedures.  The
  108.  *    caller should use to varargs macros to extract the format
  109.  *    string and the va_list structure.  This also checks for
  110.  *    recursion that can result from a panic and initializes
  111.  *    the stream data structure needed by the standard vfprintf.
  112.  *
  113.  * Results:
  114.  *      Number of characters printed.
  115.  *
  116.  * Side effects:
  117.  *      None.
  118.  *
  119.  * ----------------------------------------------------------------------------
  120.  */
  121.  
  122. #ifdef lint
  123. /* VARARGS1 */
  124. /* ARGSUSED */
  125. int vprintf(format)
  126.     char *format;
  127. {
  128.     /*
  129.      * Lint complains about unused variables...  This is all #ifdef'ed lint.
  130.      * It's silly and can probably be cut down a bit....
  131.      */
  132.     char foo;
  133.     Sync_Semaphore *barPtr;
  134.     barPtr = &sysPrintMutex;
  135.     sysPrintMutex = *barPtr;
  136.     writeProc((FILE *) NULL, 0);
  137.     streamBuffer[0] = '\0';
  138.     foo = streamBuffer[0];
  139.     streamBuffer[0] = foo;
  140. }
  141. #else
  142. /*VARARGS1*/
  143. int
  144. vprintf(format, args)
  145.     char    *format;
  146.     va_list    args;
  147. {
  148.     static Boolean    initialized = FALSE;
  149.     static FILE        stream;
  150.     static int    recursiveCallP = 0;    /* prevent recursive calls
  151.                      * that could occur if vprintf
  152.                      * fails, etc.  */
  153.  
  154.     if (recursiveCallP != 0) {
  155.     return 0;
  156.     }
  157.     recursiveCallP = 1;
  158.     MASTER_LOCK(&sysPrintMutex);
  159.     if (!initialized) {
  160.     Stdio_Setup(&stream, 0, 1, streamBuffer, STREAM_BUFFER_SIZE,
  161.         (void (*)()) 0, writeProc,  (int (*)()) 0, (ClientData) 0);
  162.     initialized = TRUE;
  163.     }
  164.  
  165.     bytesWritten = 0;
  166.     if (!sys_DontPrint) {
  167.     vfprintf(&stream, format, args);
  168.     }
  169.     fflush(&stream);
  170.     MASTER_UNLOCK(&sysPrintMutex);
  171.     recursiveCallP = 0;
  172.     return (bytesWritten);
  173.  
  174. }
  175. #endif
  176.  
  177.  
  178. /* 
  179.  *----------------------------------------------------------------------
  180.  *
  181.  * panic --
  182.  *
  183.  *    Print an error message and enter the debugger. This entry is 
  184.  *    provided for libc.a routines.
  185.  *
  186.  * Results:
  187.  *    None.
  188.  *
  189.  * Side effects:
  190.  *    The kernel dies, entering the debugger if possible.
  191.  *
  192.  *----------------------------------------------------------------------
  193.  */
  194.  
  195. #ifdef lint
  196. /* VARARGS1 */
  197. /* ARGSUSED */
  198. void panic(format)
  199.     char *format;
  200. {}
  201. #else
  202. void
  203. panic(va_alist)
  204.     va_dcl            /* char *format, then any number of additional
  205.                  * values to be printed under the control of
  206.                  * format.  This is all just the same as you'd
  207.                  * pass to printf. */
  208. {
  209.     char *format;
  210.     va_list args;
  211.  
  212.     va_start(args);
  213.     format = va_arg(args, char *);
  214.  
  215.     if (!main_PanicOK || Mach_AtInterruptLevel()) {
  216.     Mach_MonPrintf("Fatal Error: ");
  217.     Mach_MonPrintf(format, args);
  218.     }
  219.     Dev_VidEnable(TRUE);    /* unblank the screen */
  220.     Dev_SyslogDebug(TRUE);    /* divert /dev/syslog output to the screen */
  221.     if (!sysPanicing) {
  222.     sysPanicing = TRUE;
  223.     printf("Fatal Error: ");
  224.     (void) vprintf(format, args);
  225.     va_end(args);
  226.     }
  227.     DBG_CALL;
  228.     Dev_SyslogDebug(FALSE);
  229. }
  230. #endif
  231.  
  232. /*
  233.  * ----------------------------------------------------------------------------
  234.  *
  235.  * printf --
  236.  *
  237.  *      Perform a C style printf with disabling of interrupts.
  238.  *
  239.  * Results:
  240.  *      None.
  241.  *
  242.  * Side effects:
  243.  *      None.
  244.  *
  245.  * ----------------------------------------------------------------------------
  246.  */
  247.  
  248.  
  249. #ifdef lint
  250. /* VARARGS1 */
  251. /* ARGSUSED */
  252. void printf(format)
  253.     char *format;
  254. {}
  255. #else
  256. void
  257. printf(va_alist)
  258.     va_dcl
  259. {
  260.     char *format;
  261.     va_list    args;
  262.  
  263.     va_start(args);
  264.     format = va_arg(args, char *);
  265.  
  266.     (void) vprintf(format, args);
  267.     va_end(args);
  268. }
  269. #endif
  270.  
  271. /*
  272.  * ----------------------------------------------------------------------------
  273.  *
  274.  * fprintf --
  275.  *
  276.  *      Perform a C style fprintf with disabling of interrupts (output
  277.  *    always goes to the console:  stream arg is ignored).
  278.  *
  279.  * Results:
  280.  *      None.
  281.  *
  282.  * Side effects:
  283.  *      None.
  284.  *
  285.  * ----------------------------------------------------------------------------
  286.  */
  287.  
  288. #ifdef lint
  289. /* VARARGS */
  290. /* ARGSUSED */
  291. int fprintf()
  292. {}
  293. #else
  294. /*
  295. int
  296. fprintf(va_alist)
  297.     va_dcl
  298. {
  299.     char *format;
  300.     va_list    args;
  301.     int result;
  302.  
  303.     va_start(args);
  304.     (void) va_arg(args, FILE *);
  305.     format = va_arg(args, char *);
  306.  
  307.     result = vprintf(format, args);
  308.     va_end(args);
  309.     return result;
  310. }
  311. */
  312. #endif
  313.